﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameEngine
{
    /// <remarks>
    ///     Used to play looped music and efects.   
    /// </remarks>
    public class SoundManager:FrameworkElement
    {

        /// <summary>
        ///     Whether the sound should be played in a loop.
        /// </summary>
        private bool _loop = false;
        private Uri _source = null; 


        /// <summary>
        ///     Plays the selected music file in loop.
        /// </summary>
        /// <param name="fileName">Path to the file to be played.</param>
        /// <param name="volume">Volume.</param>
        public void PlayLooped(string fileName, double volume)
        {
            _source = new Uri(fileName, UriKind.RelativeOrAbsolute);
            MediaElement loopedMedia = new MediaElement();
            loopedMedia.CurrentStateChanged += new RoutedEventHandler(Loop_Audio);
            loopedMedia.Source = _source;
            loopedMedia.Volume = volume;
            loopedMedia.AutoPlay = true;
            _loop = true;
            (GameEngine.App.Current.RootVisual as MainPage).LayoutRoot.Children.Add(loopedMedia);
        }


        /// <summary>
        ///     Loops the music.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Loop_Audio(object sender, RoutedEventArgs e)
        {
            if (sender is MediaElement)
            {
                MediaElement loopedMedia = sender as MediaElement;
                if (loopedMedia.CurrentState == MediaElementState.Paused && _loop)
                {
                    loopedMedia.Source = _source;
                    loopedMedia.Position = new TimeSpan(0);
                    loopedMedia.Play();
                }
            }   
        }


        /// <summary>
        ///     Plays an effect and when done, removes the media element used.
        /// </summary>
        /// <param name="fileName">Path to the file to be played.</param>
        /// <param name="volume">Volume-different effects can have different volume.</param>
        public static void QuickPlay(string fileName, double volume)
        {
            MediaElement oneTimeMedia = new MediaElement();
            oneTimeMedia.CurrentStateChanged += new RoutedEventHandler(AudioState_Changed);
            oneTimeMedia.Source = new Uri(fileName, UriKind.RelativeOrAbsolute);
            oneTimeMedia.Volume = volume;
            oneTimeMedia.AutoPlay = true;
            (GameEngine.App.Current.RootVisual as MainPage).LayoutRoot.Children.Add(oneTimeMedia);
        }


        /// <summary>
        ///     Plays an effect with full volume and when done, removes the media element used.
        /// </summary>
        /// <param name="fileName">name of the file to be played</param>
        public static void QuickPlay(string fileName)
        {
            QuickPlay(fileName, 1.0);
        }


        /// <summary>
        ///     Event handler used to remove the element after the playback's complete.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void AudioState_Changed(object sender, RoutedEventArgs e)
        {
            if (sender is MediaElement)
            {
                MediaElement oneTimeMedia = sender as MediaElement;
                if (oneTimeMedia.CurrentState == MediaElementState.Paused)
                {
                    oneTimeMedia.CurrentStateChanged -= new RoutedEventHandler(AudioState_Changed);
                    (GameEngine.App.Current.RootVisual as MainPage).LayoutRoot.Children.Remove(oneTimeMedia); 
                }
            }
        }
    }
}
